<?php

/**
 * ECSHOP 会员中心
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: user.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ( empty($_SESSION['user_id']))
{
    header('Location:/');exit;
}

define('SITE_URL','');
$smarty->assign('IMG_PATH',SITE_URL.'/images/pc/');
$smarty->assign('JS_PATH',SITE_URL.'/js/pc/');
$smarty->assign('CSS_PATH',SITE_URL.'/css/pc/');
$smarty->caching = false;


$user_id = $_SESSION['user_id'];
$action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';

$affiliate = unserialize($GLOBALS['_CFG']['affiliate']);
$smarty->assign('affiliate', $affiliate);
$back_act='';

if($action=='add_order')
{

    $mobile = !empty($_POST['mobile']) ? $_POST['mobile'] : '';
    $password = !empty($_POST['password']) ? $_POST['password'] : '';
    if (empty($mobile) || empty($password))
    {
        exit(returnFail('请正确输入手机号或密码'));
    }
    else
    {
        $info = $user->check_user($mobile, $password);
        if ($info['status'] == 'ok' )
        {
            $user->set_session($mobile);
            $user->set_cookie($mobile);
            exit(returnSuccess());
        }
        else
        {
            exit(returnFail($info['msg'],$info['type']));
        }
    }
    exit;
}
//注册
elseif($action=='register')
{
    include_once(ROOT_PATH . 'includes/lib_passport.php');

    $phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
    $password = isset($_POST['password']) ? trim($_POST['password']) : '';
    $code   = isset($_POST['code']) ? trim($_POST['code']) : '';

    $info = $user->check_user($phone);
    if( !$info ) {
        $tag=false;
        if(!empty($_SESSION['sms_code_'.$phone])){
            foreach($_SESSION['sms_code_'.$phone] as $k=>$v) {
                if($code==$v) $tag=true;
            }
        }
        /*if( !$tag ) {
            print_r(returnFail('验证码错误', 'code_error'));
            exit;
        }*/
        if(str_len($password)<6)
        {
            exit(returnFail('密码不应小于6位', 'password_error'));
        }

        if (!$GLOBALS['user']->add_user($phone,$phone, $password, '' )) {
            print_r(returnFail($GLOBALS['user']->error, 'reg_error'));
        }else{
            if( $item_info ) $_SESSION['item_info'] = $item_info;
            print_r(returnSuccess());
        }
    }else{
        print_r(returnFail('此手机号已经存在', 'isexist'));
    }
    exit;
}else if($action=='logout')
{
    $user->logout();
    echo "<script>history.go(-1);</script>";exit;
}

// 不需要登录的操作或自己验证是否登录（如ajax处理）的act
$not_login_arr =
    array('login','act_login','register','act_register','act_edit_password','get_password','send_pwd_email','password', 'signin', 'add_tag', 'collect', 'return_to_cart', 'logout', 'email_list', 'validate_email', 'send_hash_mail', 'order_query', 'is_registered', 'check_email','clear_history','qpassword_name', 'get_passwd_question', 'check_answer');

/* 显示页面的action列表 */
$ui_arr = array('register', 'login', 'profile', 'order_list', 'order_detail', 'address_list', 'collection_list',
                'message_list', 'tag_list', 'get_password', 'reset_password', 'booking_list', 'add_booking', 'account_raply',
                'account_deposit', 'account_log', 'account_detail', 'act_account', 'pay', 'default', 'bonus', 'group_buy', 'group_buy_detail', 'affiliate', 'comment_list','validate_email','track_packages', 'transform_points','qpassword_name', 'get_passwd_question', 'check_answer');

/* 未登录处理 */
/*if (empty($_SESSION['user_id']))
{
    if (!in_array($action, $not_login_arr))
    {
        if (in_array($action, $ui_arr))
        {
            if (!empty($_SERVER['QUERY_STRING']))
            {
                $back_act = 'user.php?' . strip_tags($_SERVER['QUERY_STRING']);
            }
            $action = 'login';
        }
        else
        {
            //未登录提交数据。非正常途径提交数据！
            die($_LANG['require_login']);
        }
    }
}*/


/**
 * ----------------------------------------
 * |下面是需要登录的操作
 * ----------------------------------------
 */


/* 如果是显示页面，对页面进行相应赋值 */
if (in_array($action, $ui_arr))
{
    assign_template();
    $position = assign_ur_here(0, $_LANG['user_center']);
    $smarty->assign('page_title', $position['title']); // 页面标题
    $smarty->assign('ur_here',    $position['ur_here']);
    $sql = "SELECT value FROM " . $ecs->table('shop_config') . " WHERE id = 419";
    $row = $db->getRow($sql);
    $car_off = $row['value'];
    $smarty->assign('car_off',       $car_off);
    /* 是否显示积分兑换 */
    if (!empty($_CFG['points_rule']) && unserialize($_CFG['points_rule']))
    {
        $smarty->assign('show_transform_points',     1);
    }
    $smarty->assign('helps',      get_shop_help());        // 网店帮助
    $smarty->assign('data_dir',   DATA_DIR);   // 数据目录
    $smarty->assign('action',     $action);
    $smarty->assign('lang',       $_LANG);
}


//用户中心欢迎页
if ($action == 'default')
{

    include_once(ROOT_PATH .'includes/lib_clips.php');
    if ($rank = get_rank_info())
    {
        $smarty->assign('rank_name', sprintf($_LANG['your_level'], $rank['rank_name']));
        if (!empty($rank['next_rank_name']))
        {
            $smarty->assign('next_rank_name', sprintf($_LANG['next_level'], $rank['next_rank'] ,$rank['next_rank_name']));
        }
    }
    $smarty->assign('info',        get_user_default($user_id));
    $smarty->assign('user_notice', $_CFG['user_notice']);
    $smarty->assign('prompt',      get_user_prompt($user_id));
    //$smarty->display('user_clips.dwt');
    $smarty->display('order_list.html');
}elseif($action=='changPassword')
{
    $smarty->display('change_password.html');exit;
}
//订单详情
elseif($action=='orderDetail')
{
    $smarty->display('order_detail.html');exit;
}
//加入订单
elseif($action=='add_order')
{
    $smarty->display('add_order.html');exit;
}

exit;
